package com.chen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.entity.Cascader;
import com.chen.mapper.CascaderMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author personcoder007
 * @since 2022-11-05
 */
@RestController
@RequestMapping("/cascader")
@Api(value = "CascaderController",tags = "级联选择器")
public class CascaderController {



    @Autowired
    private CascaderMapper cascaderMapper;

    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有")
    public  Result findAll(){
//        查询所有的
        List<Cascader> cascaderList = cascaderMapper.selectList(null);
//        一级
        List<Cascader> collect = cascaderList.stream().filter(m -> m.getPid() == null).collect(Collectors.toList());
        for (Cascader cascader : collect) {
            List<Cascader> collect1 = cascaderList.stream().filter(s ->cascader.getId().equals(s.getPid())).collect(Collectors.toList());
            cascader.setChildren(collect1);
            for (Cascader cascader1 : collect1) {
                List<Cascader> collect2 = cascaderList.stream().filter(s ->cascader1.getId().equals(s.getPid())).collect(Collectors.toList());
                cascader1.setChildren(collect2);
            }
        }


        return Result.success(collect);
    }

    @GetMapping("/findAll1")
    @ApiOperation(value = "查询所有")
    public  Result findAll1(@RequestParam(required = false) Integer pid){
        QueryWrapper<Cascader> query = new QueryWrapper();
        if (pid==null){
            query.isNull("pid");
        }else{
            query.eq("pid",pid);
        }
        List<Cascader> cascaderList = cascaderMapper.selectList(query);
        return Result.success(cascaderList);
    }


}

